/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "light.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
QueueHandle_t xKeyQueue;
QueueHandle_t xSystemQueue;
/* USER CODE END Variables */
/* Definitions for TaskKey */
osThreadId_t TaskKeyHandle;
const osThreadAttr_t TaskKey_attributes = {
  .name = "TaskKey",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityAboveNormal,
};
/* Definitions for TaskSensor */
osThreadId_t TaskSensorHandle;
const osThreadAttr_t TaskSensor_attributes = {
  .name = "TaskSensor",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityAboveNormal,
};
/* Definitions for TaskWiFi */
osThreadId_t TaskWiFiHandle;
const osThreadAttr_t TaskWiFi_attributes = {
  .name = "TaskWiFi",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for TaskDisplay */
osThreadId_t TaskDisplayHandle;
const osThreadAttr_t TaskDisplay_attributes = {
  .name = "TaskDisplay",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for TaskCtrl */
osThreadId_t TaskCtrlHandle;
const osThreadAttr_t TaskCtrl_attributes = {
  .name = "TaskCtrl",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for WiFi_txSemaphore */
osSemaphoreId_t WiFi_txSemaphoreHandle;
const osSemaphoreAttr_t WiFi_txSemaphore_attributes = {
  .name = "WiFi_txSemaphore"
};
/* Definitions for WiFi_rxSemaphore */
osSemaphoreId_t WiFi_rxSemaphoreHandle;
const osSemaphoreAttr_t WiFi_rxSemaphore_attributes = {
  .name = "WiFi_rxSemaphore"
};
/* Definitions for Debug_txSemaphore */
osSemaphoreId_t Debug_txSemaphoreHandle;
const osSemaphoreAttr_t Debug_txSemaphore_attributes = {
  .name = "Debug_txSemaphore"
};
/* Definitions for Debug_rxSemaphore */
osSemaphoreId_t Debug_rxSemaphoreHandle;
const osSemaphoreAttr_t Debug_rxSemaphore_attributes = {
  .name = "Debug_rxSemaphore"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void Task_Key(void *argument);
void Task_Sensor(void *argument);
void Task_WiFi(void *argument);
void Task_Display(void *argument);
void Task_Ctrl(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* creation of WiFi_txSemaphore */
  WiFi_txSemaphoreHandle = osSemaphoreNew(1, 1, &WiFi_txSemaphore_attributes);

  /* creation of WiFi_rxSemaphore */
  WiFi_rxSemaphoreHandle = osSemaphoreNew(1, 0, &WiFi_rxSemaphore_attributes);

  /* creation of Debug_txSemaphore */
  Debug_txSemaphoreHandle = osSemaphoreNew(1, 1, &Debug_txSemaphore_attributes);

  /* creation of Debug_rxSemaphore */
  Debug_rxSemaphoreHandle = osSemaphoreNew(1, 0, &Debug_rxSemaphore_attributes);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
	xKeyQueue  = xQueueCreate(5, sizeof(KeyMenuEvent_t));
	xSystemQueue = xQueueCreate(5, sizeof(sensor_data_t));

  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of TaskKey */
  TaskKeyHandle = osThreadNew(Task_Key, NULL, &TaskKey_attributes);

  /* creation of TaskSensor */
  TaskSensorHandle = osThreadNew(Task_Sensor, NULL, &TaskSensor_attributes);

  /* creation of TaskWiFi */
  TaskWiFiHandle = osThreadNew(Task_WiFi, NULL, &TaskWiFi_attributes);

  /* creation of TaskDisplay */
  TaskDisplayHandle = osThreadNew(Task_Display, NULL, &TaskDisplay_attributes);

  /* creation of TaskCtrl */
  TaskCtrlHandle = osThreadNew(Task_Ctrl, NULL, &TaskCtrl_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_Task_Key */
/**
  * @brief  Function implementing the TaskKey thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_Task_Key */
__weak void Task_Key(void *argument)
{
  /* USER CODE BEGIN Task_Key */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END Task_Key */
}

/* USER CODE BEGIN Header_Task_Sensor */
/**
* @brief Function implementing the TaskSensor thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Task_Sensor */
__weak void Task_Sensor(void *argument)
{
  /* USER CODE BEGIN Task_Sensor */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END Task_Sensor */
}

/* USER CODE BEGIN Header_Task_WiFi */
/**
* @brief Function implementing the TaskWiFi thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Task_WiFi */
__weak void Task_WiFi(void *argument)
{
  /* USER CODE BEGIN Task_WiFi */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END Task_WiFi */
}

/* USER CODE BEGIN Header_Task_Display */
/**
* @brief Function implementing the TaskDisplay thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Task_Display */
__weak void Task_Display(void *argument)
{
  /* USER CODE BEGIN Task_Display */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END Task_Display */
}

/* USER CODE BEGIN Header_Task_Ctrl */
/**
* @brief Function implementing the TaskCtrl thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Task_Ctrl */
__weak void Task_Ctrl(void *argument)
{
  /* USER CODE BEGIN Task_Ctrl */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END Task_Ctrl */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

